/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package actions;

import com.paris5.master2.guitarmaster.bean.EcomProductType;
import com.paris5.master2.guitarmaster.bean.EcomUser;
import com.paris5.master2.guitarmaster.exception.EcomException;
import com.paris5.master2.guitarmaster.exception.EcomQueryException;
import com.paris5.master2.guitarmaster.exception.EcomValidationException;
import com.paris5.master2.guitarmaster.service.ServiceConvertorRemote;
import com.paris5.master2.guitarmaster.service.ServiceProductRemote;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;

/**
 *
 * @author Alice
 */
public class Rubrique extends EcomAction implements ServletRequestAware {

    private List<EcomProductType> products;
    private int countProducts;
    private int idCat;
    private HttpServletRequest request;
    private String trie;

    public List<EcomProductType> getProducts() {
        return products;
    }

    public void setProducts(List<EcomProductType> products) {
        this.products = products;
    }

    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    public int getCountProducts() {
        return countProducts;
    }

    public void setCountProducts(int countProducts) {
        this.countProducts = countProducts;
    }

    public String getTrie() {
        return trie;
    }

    public void setTrie(String trie) {
        this.trie = trie;
    }

    @Override
    public String execute() {
        try {
            ServiceProductRemote serviceProductRemote = lookupServiceProductRemote();
            idCat = Integer.parseInt(request.getParameter("idCategory"));
            trie = request.getParameter("trie");
            
            if ("prixCroissant".equals(trie)) {
                products = serviceProductRemote.getProductTypesByCategory(idCat, 0, 10, 2, false);
            } else if ("prixDecroissant".equals(trie)) {
                products = serviceProductRemote.getProductTypesByCategory(idCat, 0, 10, 2, true);
            }else{
                products = serviceProductRemote.getProductTypesByCategory(idCat, 0, 10, 2, false);
            }
            countProducts = serviceProductRemote.countProductTypesByCategory(idCat);
            String msg = "";
            if (idCat == 1) {
                msg = "Guitares Electriques ("+countProducts+")";
            } else if (idCat == 2) {
                msg = "Guitares Acoustiques ("+countProducts+")";
            } else if (idCat == 3) {
                msg = "Accessoires ("+countProducts+")";
            }
            if (products == null) {
                msg = "Aucun produit";
            }
            setMessage(msg);
            registerRate();
            return SUCCESS;
        } catch (EcomValidationException ex) {
            Logger.getLogger(Rubrique.class.getName()).log(Level.SEVERE, null, ex);
        } catch (EcomQueryException ex) {
            Logger.getLogger(Rubrique.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ERROR;
    }
}
